package com.lightsh.reflect;
import com.lightsh.reflect.ReflectHelper;


public class ReflectTester {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			ClassA a = (ClassA)ReflectHelper.reflectConstructor(ClassA.class, null);
			System.out.println(a.toString());
			System.out.println(a.toString("taga"));
			
			System.out.println(ReflectHelper.invoke(a, "toString", null));
			System.out.println(ReflectHelper.invoke(a, "toString", new Object[]{"MyTaga"}));
			
			
			ClassA b = (ClassA)ReflectHelper.reflectConstructor(ClassA.class, new Class<?>[]{String.class, int.class}, new Object[]{"lightsh", 9});
			System.out.println(b.toString());
			System.out.println(b.toString("tagb"));
			
			// 设置变量值
			ClassB c = new ClassB();
			ReflectHelper.setField(c, "mCount", 1);
			ReflectHelper.setField(c, "mName", "set by setField");
			ReflectHelper.setField(c, "mSecoudCount", 2);
			
			System.out.println(c.toString());
			
			// 获取变量值
			System.out.println("mCount = " + ReflectHelper.getField(c, ClassA.class, "mCount").toString());
			System.out.println("mName = " + ReflectHelper.getField(c, ClassA.class, "mName").toString());
			System.out.println("mSecoudCount = " + ReflectHelper.getField(c, c.getClass(), "mSecoudCount").toString());
			
			
			// 访问静态方法
			System.out.println(ClassA.class.getName());
			System.out.println(ReflectHelper.invokeStatic(ClassA.class.getName(), "getString", null));
			System.out.println(ReflectHelper.invokeStatic(ClassA.class.getName(), "getString", new String[]{"parameter1"}));
			System.out.println(ReflectHelper.invokeStatic(ClassA.class.getName(), "getString", new Class[]{String.class}, new String[]{"parameter2"}));
			
			System.out.println(ReflectHelper.getStaticField(ClassA.class.getName(), "STATIC_FIELD"));
			System.out.println(ReflectHelper.setStaticField(ClassA.class.getName(), "STATIC_FIELD", "NEW_STATIC_value"));
			System.out.println(ReflectHelper.getStaticField(ClassA.class.getName(), "STATIC_FIELD"));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static class ClassA{
		public ClassA(){
			mName = "not init";
			mCount = 0;
		}
		public ClassA(String name, int count){
			mName = name;
			mCount = count;
		}
		String mName;
		int mCount;
		public String toString(){
			
			return mName + ":" + mCount;
		}
		public String toString(String tag){
			return tag + ";" + toString();
		}
		public static String getString(){
			return ClassA.class.getName() + " test";
		}
		public static String getString(String name){
			return ClassA.class.getName() + " " + name;
		}
		
		private static String STATIC_FIELD = "STATIC_FIELD";
	}
	
	public static class ClassB extends ClassA{
		private int mSecoudCount;
		public ClassB(){
			super();
			mName = "not init";
			mCount = 0;
			mSecoudCount = 0;
		}
		public ClassB(String name, int count, int secondCount){
			mName = name;
			mCount = count;
			mSecoudCount = secondCount;
		}
		public String toString(){			
			return mName + ":" + mCount + "," + mSecoudCount;
		}
		public String toString(String tag){
			return tag + ";" + toString();
		}
	}
}
